/*
 Copyright (C) 2011  Matteo Magnani, Gabriele Nunziante
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


import java.sql.*;
import java.util.*;

public class Database {
	protected String IP;			// database server address
	protected String dbName;      
	protected Properties info;
	protected String error;       // Info on last exception
	protected Connection db;      
	protected boolean connected;    // Indicates if the connection is active or not
	
	public Database(String dbName) {
		this(dbName, "", ""); 
	}
	
	public Database(String dbName, String userName, String userPWD) {
		this.IP = "localhost";
		this.dbName = dbName;
		info = new Properties(); 
		info.put("user", userName); 
		info.put("password", userPWD); 
		connected = false;
		error = "";
	}
	
	public Database(String IPDB, String dbName, String userName, String userPWD) {
		this.IP = IPDB;
		this.dbName = dbName;
		info = new Properties(); 
		info.put("user", userName); 
		info.put("password", userPWD); 
		connected = false;
		error = "";
	}
	
	public Database(String IPDB, String dbName, String userName, String userPWD, String characterEncoding) {
		this.IP = IPDB;
		this.dbName = dbName;		
		info = new Properties(); 
		info.put("user", userName); 
		info.put("password", userPWD); 
		info.put("useUnicode", "true" ); 
		info.put("characterEncoding", characterEncoding ); 
		connected = false;
		error = "";
	}
	
	
	// opens the connection with the DB
	public boolean open() throws SQLException {
		connected = false;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			
			db = DriverManager.getConnection("jdbc:mysql://"+IP+"/" + dbName, info);
			
			db.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
			
			connected = true;
			
		} catch (Exception e) { error = e.getMessage(); }
		
		
		return connected;
	}
	
	public ResultSet executeQuery(String query) {
		try {
			
			Statement stmt = db.createStatement();
			
			ResultSet rs = stmt.executeQuery(query);
			
			return rs;
			
		} catch (Exception e) { e.printStackTrace(); error = e.getMessage(); }
		
		return null;
	}
	
	public boolean executeUpdate(String query) {
		int number = 0;
		boolean result = false;
		try {
			
			Statement stmt = db.createStatement();
			number = stmt.executeUpdate(query);
			
			result = true;
			stmt.close();
			
		} catch (Exception e) {
			e.printStackTrace();
			error = e.getMessage();
			result = false;
		}
		
		return result;
	}
	
	public void disconnect() {
		try {
			db.close();
			connected = false;
		} catch (Exception e) { }
	}
	
	public boolean isConnected() { 
		return connected; 
	}
	
	public String getError() { 
		return error; 
	}
}
